package sd.czw.com.mmgapp.wxapi;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import sd.czw.com.mmgapp.Contants;


/**
 * Created by czw
 * desc 描述：
 * on 2017/5/17.
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;
    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, Contants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        switch (baseResp.errCode){
            case 0:
                final String code = ((SendAuth.Resp) baseResp).code;
//                OkHttpUtils.post().url("http://dog.dreamsea6d.com/app/index.php?i=2&c=entry&m=pet&do=user.loginWx")
//                        .addParams("time", Contants.TIME).addParams("sign",Contants.SIGN).addParams("code",code)
//                        .build().execute(new WxLoginRequestCallback() {
//                    @Override
//                    public void onError(Call call, Exception e, int id) {
//
//                    }
//
//                    @Override
//                    public void onResponse(final WxLoginModel response, int id) {
//                        if (response.getErrcode() == 1051){
//                            final AlertDialog.Builder builder = new AlertDialog.Builder(WXEntryActivity.this);
//                            builder.setTitle("此微信尚未绑定");
//                            builder.setMessage("推荐使用此微信进行绑定，此微信账号将会与宠物保姆进行绑定，更方便使用");
//                            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                                @Override
//                                public void onClick(DialogInterface dialogInterface, int i) {
//                                    Intent intent = new Intent(WXEntryActivity.this,RegisterActivity.class);
//                                    intent.putExtra("log","wxlog");
//                                    intent.putExtra("wxkey",response.getErrmsg().getWxkey());
//                                    startActivity(intent);
//                                    finish();
//                                }
//                            });
//                            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                                @Override
//                                public void onClick(DialogInterface dialogInterface, int i) {
//                                    dialogInterface.dismiss();
//                                    finish();
//                                }
//                            });
//                            builder.show();
//                        }else if (response.getErrcode() == 0){
//                            SpUtils spUtils = new SpUtils(WXEntryActivity.this,"token");
//                            SpUtils spUtils2 = new SpUtils(WXEntryActivity.this,"uid");
//                            spUtils.putString("token",response.getErrmsg().getToken());
//                            spUtils2.putString("uid",response.getErrmsg().getUid());
//                            Toast.makeText(WXEntryActivity.this, "微信登录成功", Toast.LENGTH_SHORT).show();
//                            onBackPressed();
//                        }
//                    }
//                });
                break;
            case -4:
                Toast.makeText(WXEntryActivity.this, "拒绝授权", Toast.LENGTH_SHORT).show();
                finish();
                break;
            case -2:
                Toast.makeText(WXEntryActivity.this, "取消微信登录", Toast.LENGTH_SHORT).show();
                finish();
                break;
        }

    }

}
